package dev.admin.controller;

import dev.admin.model.web.GridModel;
import dev.admin.service.SysUserService;
import dev.common.base.BaseController;
import dev.common.base.ResModel;
import dev.common.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: JoyDev
 * @description: 用户表控制器
 * @author: MR.C
 * @create: 2018-02-07 21:29
 **/
@Controller
@RequestMapping("/admin/user")
public class SysUserController extends BaseController {

    @Autowired
    SysUserService userService;

    @RequestMapping(value = {"", "index"})
    public String index(Model model) {
        List all = userService.findAll();
        model.addAttribute("users", all);
        return "sysUser/sysUserIndex";
    }

    @RequestMapping("grid")
    @ResponseBody
    public GridModel list(HttpServletRequest request) {
        return userService.getGridData(request);
    }

    @RequestMapping("form/{id}")
    public String form(@PathVariable Integer id, Model model) {
        SysUser user;
        if (id == 0) {
            user = new SysUser();
        } else {
            user = userService.findByPrimaryKey(id);
        }
        model.addAttribute("user", user);
        return "sysUser/sysUserAddEdit";
    }

    @RequestMapping("save")
    @ResponseBody
    public ResModel save(@ModelAttribute("user") SysUser user) throws Exception {
        int i = userService.saveData(user);
        return sendMessage2Web(true, "保存成功");
    }

    @RequestMapping("update")
    @ResponseBody
    public ResModel update(@ModelAttribute("user") SysUser user) throws Exception {
        int i = userService.updateData(user);
        return sendMessage2Web(true, "更新成功");
    }

    @RequestMapping("delete")
    @ResponseBody
    public ResModel delete(@RequestParam String ids) {
        userService.deleteByIds(ids);
        return sendMessage2Web(true, "删除成功");
    }

}
